package hxm.monads;

/**
 * The OptionMonad lets you work on None/Some options.
 * 
 * @author Simon Krajewski
 */

class OptionMonad 
{
	static public function bind<A, B>(a:Option<A>, f:A -> Option<B>):Option<B>
	{
		switch(a)
		{
			case Some(t):
				return f(t);
			case None:
				return None;
		}
	}
	
	inline static public function map<A, B>(a:Option<A>, f:A->B):Option<B>
	{
		switch(a)
		{
			case Some(t):
				return Some(f(t));
			case None:
				return None;
		}
	}
	
	inline static public function yield<A>(a:A):Option<A>
	{
		return Some(a);
	}

	static public var zero = None;
}

enum Option<T>
{
	Some(t:T);
	None;
}
